var _ = require('lodash');
var yaml = require('js-yaml');
var fs   = require('fs');

module.exports = function (file) {
    // Get document, or throw exception on error
    try {
        var style = yaml.safeLoad(fs.readFileSync(file, 'utf8'));
        console.log(style);
    } catch (e) {
        console.log(e);
    }

    function selector_extend(styles, parent) {
        return Object.keys(styles).map(function (v) {
            return  parent + " " + v;
        });
    }

    function combine_keys_values(keys, values) {
        var ret = {};
        keys.forEach(function (key, index) {
            ret[key] = values[index]
        });
        return ret;
    }

    function handleDollar(scss) {
        var keys = [];
        var values = [];
        var obj = {};
        var parentkeys = Object.keys(scss);
        _.values(scss).forEach(function (styles, index) {
            var stylemaps = [];
            var stylekeys = Object.keys(styles);
            _.values(styles).filter(function (v, index) {
                if (_.isObject(v)) {
                    stylemaps.push({
                        key: stylekeys[index],
                        value: v
                    });
                }
            });
            keys = keys.concat(stylemaps.map(function (v) {
                return parentkeys[index] + " " + v.key.replace(/&/g, parentkeys[index]);
            }));
            values = values.concat(stylemaps.map(function (v) {
                return v.value;
            }));
        });
        obj = combine_keys_values(keys, values);
        return {
            keys,
            values,
            obj
        }
    }

    function has_object_value(arr) {
        var length = 0;
        arr.forEach(function (item) {
            length += _.values(item).filter(function (v, index) {
                return _.isObject(v);
            }).length ;
        });
        return length > 0;
    }

    function check_obj_has_obj(obj) {
        return _.values(obj).filter(function (v) {
                return _.isObject(v);
            }).length > 0;
    }

    var resolvestyles = [];

    function filter_not_has_object_style(scss) {
        var ret = {};
        var reskeys = [];
        var keys = Object.keys(scss);
        _.values(scss).forEach(function (styles, index) {
            if (!check_obj_has_obj(styles)) {
                reskeys.push(keys[index]);
            } else {
                ret[keys[index]] = _.pick(styles, Object.keys(styles).filter(function (prop) {
                    return !_.isObject(styles[prop]);
                }));
            }
        });
        reskeys.forEach(function (v) {
            ret[v] = scss[v];
        });
        return {
            isready: ret
        };
    }

    function resolve_style_map(scss) {
        var ret = handleDollar(scss);
        var splitstyles = filter_not_has_object_style(ret.obj);
        resolvestyles.push(splitstyles.isready);
        if (ret.values.length > 0 && has_object_value(ret.values)) {
            resolve_style_map(ret.obj);
        }
    }

    resolvestyles.push(filter_not_has_object_style(style.scss).isready);

    resolve_style_map(style.scss);

    var inputStyle = {};

    resolvestyles.forEach(function (resolvestyleschunk, index) {
        Object.keys(resolvestyleschunk).forEach(function (selector) {
            inputStyle[selector] = resolvestyleschunk[selector];
        });
    });
    return {
        inputStyle: inputStyle
    }
};